package com.bluesea.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bluesea.dao.UserDao;
import com.bluesea.exception.BusinessException;
import com.bluesea.model.User;
import com.bluesea.service.UserService;


@Service(value = "userService")
public class UserServiceImpl implements UserService {

	private Log log = LogFactory.getLog(UserServiceImpl.class);

	@Autowired
	private UserDao userDao;

	public void addUser(User user) throws BusinessException {
		// TODO Auto-generated method stub
		try {
			userDao.create(user);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("Method userDao.create() failure, paras{username:"
					+ user.getUsername() + ", password:" + user.getPassword()
					+ "}." + e.getMessage());
			throw new BusinessException(
					"Method userDao.create() failure, paras{username:"
							+ user.getUsername() + ", password:"
							+ user.getPassword() + "}.", e);
		}
	}

	public void deleteUser(User user) throws BusinessException {
		// TODO Auto-generated method stub
		try {
			userDao.delete(user);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("Method userDao.delete() failure, paras{username:"
					+ user.getUsername() + "}." + e.getMessage());
			throw new BusinessException(
					"Method userDao.delete() failure, paras{username:"
							+ user.getUsername() + "}.", e);
		}
	}

	public List<User> findAll() throws BusinessException {
		// TODO Auto-generated method stub
		try {
			return userDao.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			log.error("Method userDao.findAll() failure." + e.getMessage());
			throw new BusinessException("Method userDao.create() failure.", e);
		}
	}

	public void updateUser(User user) throws BusinessException {
		// TODO Auto-generated method stub
		try {
			userDao.update(user);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("Method userDao.update() failure, paras{username:"
					+ user.getUsername() + "}." + e.getMessage());
			throw new BusinessException(
					"Method userDao.update() failure, paras{username:"
							+ user.getUsername() + "}.", e);
		}
	}

	public boolean validate(String username, String password)
			throws BusinessException {
		// TODO Auto-generated method stub
		log.info("validate method invoked, paras{username:" + username
				+ ", password:" + password + "}");

		List<User> userList = this.findUsersByName(username);

		if (userList.size() == 1) {
			User user = userList.get(0);
			return user.getPassword().equals(password);
		} else {
			return false;
		}
	}

	public List<User> findUsersByName(String username) throws BusinessException {
		// TODO Auto-generated method stub
		try {
			return userDao.fetchUsersByName(username);
		} catch (Exception e) {
			// TODO: handle exception
			log
					.error("Method userDao.fetchUsersByName() failure, paras{username:"
							+ username + "}." + e.getMessage());
			throw new BusinessException(
					"Method userDao.fetchUsersByName() failure, paras{username:"
							+ username + "}.", e);
		}
	}
}
